<?php
namespace Wap\Controller;
use Think\Controller;

class CustomerAddrController extends Controller
{
    private $zjdailyCardCustomer;
    public function __construct()
    {
        parent::__construct();
        $this->zjdailyCardCustomer = session( "zjdailyCardCustomer.login" );
    }
    public function home()
    {
        $customer_id = $this->zjdailyCardCustomer;

        if( !$customer_id ){
            $this->error( "用户未登录", U( 'Wap/ZJDailyCard/index' ) );
        }

        $bag = I( "post.bag", array(), "" );
        $bag['data_card'] = isset( $bag['data_card'] ) ? $bag['data_card'] : array();
        $bag['data_card']['cardnumber'] = I( "cardnumber", "", "" );
        //$bag['data_card']['password'] = I( "post.password", "", "" );
        $bag['data_card']['username'] = I( "username", "", "" );
        $bag['data_card']['telephone'] = I( "telephone", "", "" );
        $bag['data_card']['profession'] = I( "profession", "", "" );
        $bag['data_card']['province'] = I( "province", "", "" );
        $bag['data_card']['city'] = I( "city", "", "" );
        $bag['data_card']['county'] = I( "county", "", "" );
        $bag['data_card']['address'] = I( "address", "", "" );
        $bag['data_card']['metier'] = I( "metier", "", "" );

        $addresses = M( "customer_addr" )
            ->field( array(
                "esm_customer_addr.id" => "id",
                "esm_customer_addr.username" => "username",
                "esm_customer_addr.telephone" => "telephone",
                "esm_customer_addr.address" => "other",
                "esm_province.regionname" => "provience",
                "esm_city.regionname" => "city",
                "esm_region.regionname" => "region") )
            ->join( "LEFT JOIN esm_region as esm_province ON esm_customer_addr.province = esm_province.id" )
            ->join( "LEFT JOIN esm_region as esm_city ON esm_customer_addr.city = esm_city.id" )
            ->join( "LEFT JOIN esm_region as esm_region ON esm_customer_addr.county = esm_region.id" )
            ->where( array( "customer_id" => $customer_id, 'scope' => 'zjdailycard' ) )->select();

        $this->assign( "bag", $bag );
        $this->assign( "addresses", $addresses );
        $this->display( "home" );
    }

    public function add()
    {
        $customer_id = $this->zjdailyCardCustomer;
        if( !$customer_id ){
             $this->error( "用户未登录", U( 'Wap/ZJDailyCard/index' ) );
        }
        $this->display( "edit" );
    }

    public function edit()
    {
        //$customer_id = session( "zjdailyCard.customer.login" );
        $customer_id = $this->zjdailyCardCustomer;
        if( !$customer_id ){
            $this->error( "用户未登录", U( 'Wap/ZJDailyCard/index' ) );
        }

        $bag = I( "post.bag", array(), "" );
        $bag['data_card'] = isset( $bag['data_card'] ) ? $bag['data_card'] : array();
        $bag['data_card']['cardnumber'] = I( "get.cardnumber", "", "" );
        //$bag['data_card']['password'] = I( "post.password", "", "" );
        $bag['data_card']['username'] = I( "get.username", "", "" );
        $bag['data_card']['telephone'] = I( "get.telephone", "", "" );
        $bag['data_card']['profession'] = I( "get.profession", "", "" );
        $bag['data_card']['province'] = I( "get.province", "", "" );
        $bag['data_card']['city'] = I( "get.city", "", "" );
        $bag['data_card']['county'] = I( "get.county", "", "" );
        $bag['data_card']['address'] = I( "get.address", "", "" );

        $condition['id'] = I( "id", -1, "" );
        $condition['scope'] = 'zjdailycard';
        $address = M( "customer_addr" )->where( $condition )->find();

        $this->assign( "bag", $bag );
        $this->assign( "address", $address );
        $this->display();
    }

    public function save()//TODO: validate
    {
        //$data['customer_id'] = session( "zjdailyCard.customer.login" );
        $data['customer_id'] = $this->zjdailyCardCustomer;
        if( !$data['customer_id'] ){
            $this->error( "用户未登录", U( 'Wap/ZJDailyCard/index' )  );
        }
        $addr_id = I( "post.id", "", "" );

        $data['username'] = I( "post.username", "", "" );
        $data['telephone'] = I( "post.telephone", "", "" );
        $data['province'] = I( "post.provience", "", "" );
        $data['city'] = I( "post.city", "", "" );//TODO:set default value
        $data['county'] = I( "post.region", "", "" );
        $data['address'] = I( "post.address", "", "" );
        $data['scope'] = 'zjdailycard';
        //$data['customer_id'] = session( "customer.id" );

        $mode = D( "CustomerAddr" );
        $validated = $mode->create( $data );
        //$this->ajaxReturn( $validated );
        if( !$validated ){
            $result = array( "err" => 412, "msg" => $mode->getError() );
            $this->ajaxReturn( $result );
        }
        if( empty( $addr_id ) ){
            $result = $mode->addAddress( $data );
            $this->ajaxReturn( $result );
        }else{
            $result = $mode->updateAddress( $addr_id, $data );
            $this->ajaxReturn( $result );
        }
    }

    public function getAddr()
    {
        $addr_id = I( "post.addr_id", -1, "" );
        $address = M( "customerAddr" )->where( array( "id" => $addr_id, 'scope' => 'zjdailycard' ) )->getField( "address" );
        if( !!$address ){
            $result = array( "err" => 200, "address" => $address );
            $this->ajaxReturn( $result );
        }else{
            $result = array( "err" => 404, "address" => $address );
            $this->ajaxReturn( $result );
        }
    }

    public function choose()
    {
        //$customer_id = session( "zjdailyCard.customer.login" );
        $customer_id = $this->zjdailyCardCustomer;
        if( !$customer_id ){
             $this->error( "用户未登录", U( 'Wap/ZJDailyCard/index' ) );
        }
        $bag = I( "post.bag", array(), "" );
        $bag['data_card'] = isset( $bag['data_card'] ) ? $bag['data_card'] : array();
        $bag['data_card']['cardnumber'] = I( "cardnumber", "", "" );
        //$bag['data_card']['password'] = I( "post.password", "", "" );
        $bag['data_card']['username'] = I( "username", "", "" );
        $bag['data_card']['telephone'] = I( "telephone", "", "" );
        $bag['data_card']['profession'] = I( "profession", "", "" );
        $bag['data_card']['province'] = I( "province", "", "" );
        $bag['data_card']['city'] = I( "city", "", "" );
        $bag['data_card']['county'] = I( "county", "", "" );
        $bag['data_card']['address'] = I( "address", "", "" );
        $bag['data_card']['metier'] = I( "metier", "", "" );

        $addresses = M( "customer_addr" )
            ->field( array( "esm_customer_addr.id" => "id", 
                "esm_customer_addr.username" => "username", 
                "esm_customer_addr.telephone" => "telephone", 
                "esm_customer_addr.address" => "other", 
                "esm_province.regionname" => "provience", 
                "esm_city.regionname" => "city", 
                "esm_region.regionname" => "region") )
            ->join( "LEFT JOIN esm_region as esm_province ON esm_customer_addr.province = esm_province.id" )
            ->join( "LEFT JOIN esm_region as esm_city ON esm_customer_addr.city = esm_city.id" )
            ->join( "LEFT JOIN esm_region as esm_region ON esm_customer_addr.county = esm_region.id" )
            ->where( array( "customer_id" => $customer_id, 'scope' => 'zjdailycard' ) )
            ->select();

        $this->assign( "addresses", $addresses );
        $this->assign( "bag", $bag );
        $this->display( "home" );
    }
}
?>
